﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;

namespace RN.ERP.Web.UserControl
{
    public partial class UCCorpQual : System.Web.UI.UserControl
    {
        #region 属性
        /// <summary>
        /// 角色
        /// </summary>
        private string _setRole;
        public string SetRole
        {
            set
            {
                _setRole = value;
            }
        }
        /// <summary>
        /// Parentid
        /// </summary>
        private string _setParentid;
        public string SetParentid
        {
            set
            {
                _setParentid = value;
            }
        }
        /// <summary>
        /// 资质guid
        /// </summary>
        private string _setQualGuid;
        public string SetQualGuid
        {
            set
            {
                _setQualGuid = value;
            }
        }
        /// <summary>
        /// 传到前台最终选择的资质code
        /// </summary>
        public string Zzcode
        {
            get
            {
                return this.zzcode.Value;
            }
        }
        /// <summary>
        /// 传到前台最终选择的资质等级
        /// </summary>
        public string Zzdj
        {
            get
            {
                return this.zzdj.Value;
            }
        }
        private bool _enable = true;
        public bool Enable
        {
            get
            {
                return _enable;
            }
            set
            {
                _enable = value;
            }
        }
        #endregion 属性
        BasicServiceClient client = new BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 非空处理
            if (string.IsNullOrEmpty(_setParentid))
            {
                _setParentid = "0";
            }
            if (string.IsNullOrEmpty(_setRole))
            {
                _setRole = "15";
            }
            if (string.IsNullOrEmpty(_setQualGuid))
            {
                _setQualGuid = Guid.Empty.ToString();
            }
            #endregion 非空处理

            this.parentid.Value = _setParentid;
            //获取角色
            this.role.Value = _setRole;
            //获取设置的资质Guid
            this.qualguid.Value = _setQualGuid;
            //根据资质Guid获取资质基本信息
            QulaDDValue qualinfo = JsonConvert.DeserializeObject<QulaDDValue>(client.GetCorpQualByGuid(_setQualGuid, "10"));
            if (!IsPostBack)
            {
                //判断下拉是通过前台js加载还是后台加载
                if (qualinfo != null&&_setQualGuid != Guid.Empty.ToString())
                {
                    this.isJs.Value = "0";
                    ddBind(qualinfo);
                }
                else
                {
                    //前台js获取isJs的值用来判断是否进行js进行加载数据
                    this.isJs.Value = "1";
                }

            }
            if (!Enable)
            {
                zztype.Enabled = false;
                zztype2.Enabled = false;
                zzlev.Enabled = false;
            }
        }
        /// <summary>
        /// 绑定下拉菜单
        /// </summary>
        /// <param name="DDValue"></param>
        public void ddBind(QulaDDValue DDValue)
        {

            DictQualityInfo dic = new DictQualityInfo();
            IList<DictQualityInfo> dicBig = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic(_setRole, "0"));
            foreach (var item in dicBig)
            {
                item.Code = item.Code + "@" + item.Id;
            }
            this.zztype.DataSource = dicBig;
            this.zztype.DataTextField = "Title";
            this.zztype.DataValueField = "Code";
            this.zztype.SelectedValue = DDValue.dicBig;
            this.zztype.DataBind();
            zztype.Items.Insert(0, new ListItem("请选择","-1"));
            if (DDValue.dicBig != null)
            {
                IList<DictQualityInfo> dicSm = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic(_setRole, DDValue.dicBig.Split(new char[] { '@' })[1]));
                foreach (var item in dicSm)
                {
                    item.Code = item.Code + "@" + item.Id;
                }
                this.zztype2.DataSource = dicSm;
                this.zztype2.DataTextField = "Title";
                this.zztype2.DataValueField = "Code";
                this.zztype2.SelectedValue = DDValue.dicSm;
                this.zztype2.DataBind();
            }
            zztype2.Items.Insert(0, new ListItem("请选择", "-1"));
            if (DDValue.dicSm != null)
            {
                IList<DictQualityInfo> dicLev = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic(_setRole, DDValue.dicSm.Split(new char[] { '@' })[1]));
                foreach (var item in dicLev)
                {
                    item.Code = item.Code + "@" + item.Id;
                }
                this.zzlev.DataSource = dicLev;
                this.zzlev.DataTextField = "Title";
                this.zzlev.DataValueField = "Code";
                if (!string.IsNullOrEmpty(DDValue.dicLev))
                {
                    if(dicLev.Where(r=>r.Code==DDValue.dicLev).FirstOrDefault()!=null)
                    {
                       this.zzlev.SelectedValue = DDValue.dicLev;
                    }
                }
                this.zzlev.DataBind();
            }
            zzlev.Items.Insert(0, new ListItem("请选择", "-1"));
        }
    }
}